package com.jeeplus.modules.wms.util;

import com.jeeplus.common.utils.SpringContextHolder;
import com.jeeplus.modules.infor.entity.*;
import com.jeeplus.modules.infor.mapper.GoodsMapper;
import com.jeeplus.modules.sys.utils.UserUtils;
import com.jeeplus.modules.wms.entity.Outbound;
import com.jeeplus.modules.wms.entity.PickDetailed;
import com.lowagie.text.pdf.*;
import org.apache.pdfbox.multipdf.PDFMergerUtility;
import org.springframework.stereotype.Repository;
import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by DHW on   12/13.
 */
@Repository
public class PrintOrder {

    private static GoodsMapper goodsMapper = SpringContextHolder.getBean(GoodsMapper.class);
    /**
     *拣货单模版
     * @param outbound       实体（获取实体的信息填入模版）
     * @param templateFile  模版文件目录
     * @param file          要输出的文件
     * @throws Exception
     */
    public void updatePdfa(Outbound outbound,String ck,String weight,int amoun, List<PickDetailed> pickDetailedList, String templateFile, File file) throws Exception {

        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        PdfReader reader = new PdfReader(templateFile); // 模版文件目录
        FileOutputStream fos = new FileOutputStream(file);

        PdfStamper ps = new PdfStamper(reader, fos); // 生成的输出流

        BaseFont bf = BaseFont.createFont("C:/WINDOWS/Fonts/SIMYOU.TTF", BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);

        AcroFields s = ps.getAcroFields();

        //主表
        s.setFieldProperty("store", "textfont", bf, null);
        s.setFieldProperty("transportmode", "textfont", bf, null);
        s.setFieldProperty("shipmentnumbers", "textfont", bf, null);
        s.setFieldProperty("platnumber", "textfont", bf, null);
        s.setFieldProperty("unitname", "textfont", bf, null);
        s.setFieldProperty("code", "textfont", bf, null);
        s.setFieldProperty("amount", "textfont", bf, null);
        s.setFieldProperty("user", "textfont", bf, null);
        s.setFieldProperty("grossweight", "textfont", bf, null);
        s.setFieldProperty("date", "textfont", bf, null);
        //子表
        s.setFieldProperty("location1", "textfont", bf, null);
        s.setFieldProperty("location2", "textfont", bf, null);
        s.setFieldProperty("location3", "textfont", bf, null);
        s.setFieldProperty("location4", "textfont", bf, null);
        s.setFieldProperty("location5", "textfont", bf, null);

        s.setFieldProperty("name1", "textfont", bf, null);
        s.setFieldProperty("name2", "textfont", bf, null);
        s.setFieldProperty("name3", "textfont", bf, null);
        s.setFieldProperty("name4", "textfont", bf, null);
        s.setFieldProperty("name5", "textfont", bf, null);

        s.setFieldProperty("amount1", "textfont", bf, null);
        s.setFieldProperty("amount2", "textfont", bf, null);
        s.setFieldProperty("amount3", "textfont", bf, null);
        s.setFieldProperty("amount4", "textfont", bf, null);
        s.setFieldProperty("amount5", "textfont", bf, null);

        s.setFieldProperty("beishu1", "textfont", bf, null);
        s.setFieldProperty("beishu2", "textfont", bf, null);
        s.setFieldProperty("beishu3", "textfont", bf, null);
        s.setFieldProperty("beishu4", "textfont", bf, null);
        s.setFieldProperty("beishu5", "textfont", bf, null);

        s.setFieldProperty("banshu1", "textfont", bf, null);
        s.setFieldProperty("banshu2", "textfont", bf, null);
        s.setFieldProperty("banshu3", "textfont", bf, null);
        s.setFieldProperty("banshu4", "textfont", bf, null);
        s.setFieldProperty("banshu5", "textfont", bf, null);

        s.setFieldProperty("lingtou1", "textfont", bf, null);
        s.setFieldProperty("lingtou2", "textfont", bf, null);
        s.setFieldProperty("lingtou3", "textfont", bf, null);
        s.setFieldProperty("lingtou4", "textfont", bf, null);
        s.setFieldProperty("lingtou5", "textfont", bf, null);

        s.setFieldProperty("biaoshima1", "textfont", bf, null);
        s.setFieldProperty("biaoshima2", "textfont", bf, null);
        s.setFieldProperty("biaoshima3", "textfont", bf, null);
        s.setFieldProperty("biaoshima4", "textfont", bf, null);
        s.setFieldProperty("biaoshima5", "textfont", bf, null);

        s.setFieldProperty("shengchanriqi1", "textfont", bf, null);
        s.setFieldProperty("shengchanriqi2", "textfont", bf, null);
        s.setFieldProperty("shengchanriqi3", "textfont", bf, null);
        s.setFieldProperty("shengchanriqi4", "textfont", bf, null);
        s.setFieldProperty("shengchanriqi5", "textfont", bf, null);

        s.setFieldProperty("cls1", "textfont", bf, null);
        s.setFieldProperty("cls2", "textfont", bf, null);
        s.setFieldProperty("cls3", "textfont", bf, null);
        s.setFieldProperty("cls4", "textfont", bf, null);
        s.setFieldProperty("cls5", "textfont", bf, null);




        //设置值（主表）
        s.setField("store", "");
        Date date = new Date();
        s.setField("transportmode", outbound.getTransportMode());
        s.setField("shipmentnumbers", outbound.getShipmentNumbers());
        s.setField("platnumber", outbound.getPlateNumber());
        s.setField("user", UserUtils.getUserName());
        s.setField("date", sf.format(date));

        if ((outbound.getUnitName()) != null) {
            String unitname = String.valueOf(outbound.getUnitName());
            s.setField("unitname", unitname);
        } else {
            s.setField("unitname", "");
        }
        if ((outbound.getCode()) != null) {
            String code = String.valueOf(outbound.getCode());
            s.setField("code", code);
        } else {
            s.setField("code", "");
        }
        if ((outbound.getAmount()) != null){

            s.setField("amount", amoun+"");
        }else{
            s.setField("amount", "");
        }

            s.setField("grossweight", weight);

        //获取子表
        Date datesc = new Date();
        //子表

        for (int i =0; i< pickDetailedList.size(); i++) {
            int j=i+1;

            s.setField("location"+j, pickDetailedList.get(i).getLocation());
            s.setField("name"+j, pickDetailedList.get(i).getName());
            s.setField("amount"+j, pickDetailedList.get(i).getAmount());
            s.setField("banshu"+j, pickDetailedList.get(i).getTp()+"");
            s.setField("lingtou"+j, pickDetailedList.get(i).getLs()+"");
            s.setField("biaoshima"+j, pickDetailedList.get(i).getCode());
            if(pickDetailedList.get(i).getBach()!=null){
                s.setField("shengchanriqi"+j, pickDetailedList.get(i).getBach());
            }else{
                s.setField("shengchanriqi"+j, " ");
            }
            Goods goods=new Goods();
            goods.setCode(pickDetailedList.get(i).getCode());
            goods.setProject(UserUtils.getUseroffice());
            goods=goodsMapper.findList(goods).get(0);
            int bl=Integer.parseInt(goods.getProportion());
            s.setField("beishu"+j,Integer.parseInt(pickDetailedList.get(i).getAmount())/Integer.parseInt(goods.getProportion())+"" );
            s.setField("cls"+j,Integer.parseInt(pickDetailedList.get(i).getAmount())%Integer.parseInt(goods.getProportion())+"" );

        }
            s.setField("store",ck);
        ps.setFormFlattening(true); // 这句不能少

        ps.close();
        reader.close();
        fos.close();
    }

    /**
     *
     * @param folder       要合并文件的文件夹
     * @param outPdfFile    合并后的文件名
     * @param fileString    需要合并的pdf文件名集合
     * @throws Exception
     */
    public void mergePdf(String folder, String outPdfFile, List<String> fileString) throws Exception {
        PDFMergerUtility mergePdf = new PDFMergerUtility();
        List<String> filesInFolder = fileString;
        for (int i = 0; i < filesInFolder.size(); i++)
            mergePdf.addSource(folder + File.separator + filesInFolder.get(i));
        mergePdf.setDestinationFileName(outPdfFile);
        mergePdf.mergeDocuments();
    }

}
